﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using Logistics.Configuration;

namespace logistics
{
    /// <summary>
    /// 软件启动文件更新检测
    /// </summary>
    public class CheckUpload
    {
        #region File
        private string updateUrl = string.Empty;
        private string tempUpdatePath = string.Empty;//下载的服务器上的xml
        Logistics.Configuration.XmlFiles updaterXmlFiles = null;//本地xml文件
        private int availableUpdate = 0;
        string mainAppExe = "";//执行的主程序的名称
        ArrayList fileArray = new ArrayList();//需要更新

        #endregion

        #region Method
        public void CheckIsUpload(Button btnUpdate)
        {
            string localXmlFile = Application.StartupPath + "\\UpdateList.xml";
            string serverXmlFile = string.Empty;

            try
            {
                //从本地读取更新配置文件信息
                updaterXmlFiles = new XmlFiles(localXmlFile);
            }
            catch
            {
                MessageBox.Show("配置文件出错!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //获取服务器地址
            updateUrl = updaterXmlFiles.GetNodeValue("//Url");

            AppUpdater appUpdater = new AppUpdater();
            appUpdater.UpdaterUrl = updateUrl + "/UpdateList.xml";

            //与服务器连接,下载更新配置文件
            try
            {
                tempUpdatePath = Environment.GetEnvironmentVariable("Temp") + "\\" + "_" + updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value + "_" + "y" + "_" + "x" + "_" + "m" + "_" + "\\";//临时文件夹路径
                appUpdater.DownAutoUpdateFile(tempUpdatePath);
            }
            catch
            {
                MessageBox.Show("与服务器连接失败,操作超时!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            //获取更新文件列表
            Hashtable htUpdateFile = new Hashtable();

            serverXmlFile = tempUpdatePath + "\\UpdateList.xml";
            if (!File.Exists(serverXmlFile))
            {
                return;
            }

            availableUpdate = appUpdater.CheckForUpdate(serverXmlFile, localXmlFile, out htUpdateFile);
            if (availableUpdate > 0)
            {
                DialogResult result = MessageBox.Show("有最新版本，是否更新?", "软件更新", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {               
                    OpenAutoUpdateSoft();
                }
            }
            else
            {
                MessageBox.Show("没有可用的更新!", "自动更新", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnUpdate.Visible = false;
                return;
            }
        }

        private void OpenAutoUpdateSoft()
        {          
            IsMainAppRun();
        }
        #region

        #endregion

        //判断主应用程序是否正在运行
        private void IsMainAppRun()
        {
           
            mainAppExe = updaterXmlFiles.GetNodeValue("//EntryPoint");

            Process[] allProcess = Process.GetProcesses();
            foreach (Process p in allProcess)
            {
                if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())
                {             
                    string updateFile = Application.StartupPath + "\\AutoUpdate.exe";                   
                    Process.Start(updateFile);            
                    p.Kill();                  
                    break;
                }
            }
        }

        #endregion    
    }
}
